package com.woniu51.k15demo.exception;

import com.woniu51.k15demo.result.CommonResult;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 杨光
 * @date 2022年02月15日 9:31
 */

@ControllerAdvice
@RestController
public class GlobalExceptionHandler {


    @ExceptionHandler(Exception.class)
    public CommonResult<Void> exceptionHandler(Exception e){


        //打印异常堆栈
        e.printStackTrace();

        //如果是业务类型的异常
        if(e instanceof AppException){
            AppException appException = (AppException)e;
            return new CommonResult<>(appException.getErrorCode(),appException.getErrorMessage(),null);
        }
        return  new CommonResult<>(500,"服务端异常",null);
    }
}
